#!/bin/bash

LOGFILE="/tmp/system_monitor.log"
# 清空日志文件
echo -n "" >  /tmp/system_monitor.log

# 检查 mpstat 和 vmstat 命令是否存在
if ! command -v mpstat &> /dev/null; then
    echo "mpstat command not found. Please install sysstat package."
    exit 1
fi

if ! command -v vmstat &> /dev/null; then
    echo "vmstat command not found. Please install procps package."
    exit 1
fi

while true; do
  echo "Timestamp: $(date)" >> "$LOGFILE"
  echo "CPU Usage:" >> "$LOGFILE"
  mpstat 1 1 | tail -n 1 >> "$LOGFILE"
  echo "Memory Usage:" >> "$LOGFILE"
  vmstat -s | grep "total memory" >> "$LOGFILE"
  echo "------------------------" >> "$LOGFILE"
  sleep 10
done
